package dao.impl;

import dao.BaseDao;
import dao.UserDao;
import entity.User;
import java.sql.ResultSet;

public class UserDaoImpl extends BaseDao implements UserDao {

    @Override
    public User getUserByNameAndPwd(String username, String password) {
        User user=null;
        ResultSet rs=null;
        String sql=" SELECT * FROM `user` where userId=? and `password`=? ";
        try{
            rs=executeQuery(sql,username,password);
            while (rs.next()){
                user=new User();
                user.setId(rs.getInt("id"));
                user.setUserId(rs.getString("userId"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setImage(rs.getString("image"));
                user.setSex(rs.getString("sex"));
                user.setName(rs.getString("name"));
                user.setBirthday(rs.getString("birthday"));
                user.setAddress(rs.getString("address"));
                user.setEmail(rs.getString("email"));
                user.setMysite(rs.getString("mysite"));
                user.setSign(rs.getString("sign"));
                user.setCreatetime(rs.getString("createtime"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(conn,null,rs);
        }
        return user;
    }

    @Override
    public int updateUserImage(User user) {
        int i=0;
        String sql="update `user` set image =? where id=? ";
        try {
            i=executeUpdate(sql,user.getImage(),user.getId());
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(conn,null,null);
        }
        return i;
    }
}
